from django.shortcuts import render

# Create your views here.
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from rest_framework.viewsets import ReadOnlyModelViewSet

from areas.models import Area
from areas.serializers import AreaSerializer, SubsAreaSerializer


class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """省市区行政区划信息查询视图集"""

    pagination_class = None  # 区划信息不分页

    def get_queryset(self):
        """提供数据集"""
        if self.action == 'list':  # 如果是list行为表示是所有省的模型
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        """提供序列化器"""

        if self.action == 'list':
            return AreaSerializer
        else:
            return SubsAreaSerializer
